﻿using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.款式异常;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.UC.门店下单.service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.款式管理
{
    public partial class Frm款式维护设计点导入 : DevExpress.XtraEditors.XtraForm
    {
        private String styleNameCn { get; set; }
        
        public Frm款式维护设计点导入(String styleNameCn)
        {
            InitializeComponent();
            this.styleNameCn = styleNameCn;
        }

        private void Frm款式维护设计点导入_Load(object sender, EventArgs e)
        {
            this.splashScreenManager1.ShowWaitForm();
            this.splashScreenManager1.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager1.SetWaitFormDescription("正在初始化.....");　　　　　// 信息
            this.label提示.Text = "当前款式为：" + this.styleNameCn;
            this.刷新款式设计点(this.styleNameCn);
            this.splashScreenManager1.CloseWaitForm();
        }

        private void 刷新款式设计点(String styleNameCn)
        {
            this.gridControl设计点一览.DataSource = 
                下单相关Service.getStyleDesignImportRecordByStyleNameCN(styleNameCn);
            this.gridView设计点一览.RefreshData();
        }

        private void simpleButton上传_Click(object sender, EventArgs e)
        {
            //1.读excel
            ExcelAndPath excelAndPath = ExcelUtil.getExcelDatasAndPath();
            if (excelAndPath == null)
            {
                return;
            }
            DataSet result = excelAndPath.DataSet;
            List<设计点Excel格式> Exceldtos = new List<设计点Excel格式>();
            result.Tables[0].Rows.RemoveAt(0);
            foreach (DataRow dr in result.Tables[0].Rows)
            {
                if (!String.IsNullOrWhiteSpace(dr[0].ToString()))
                {
                    Exceldtos.Add(new 设计点Excel格式(dr, 1));
                }
            }
            //2. excel读取的数据再转成接口需要的DTOlist
            List<StyleDesignExcelDTO> styleDesignExcelDTOs = new List<StyleDesignExcelDTO>();
            foreach (设计点Excel格式 item in Exceldtos)
            {
                styleDesignExcelDTOs.Add(new StyleDesignExcelDTO(item));
            };
            new Form设计点导入预览(this.styleNameCn, styleDesignExcelDTOs).ShowDialog();
            this.刷新款式设计点(this.styleNameCn);
        }

        private void simpleButton下载_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*xlsx";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                try
                {
                    //DataTable dt = DataTableExtend.ToDataTable<设计点Excel格式>(ItemService.ListForExcel(this.设计点图片Dtos));
                    //DataTableExtend.SaveAsXlS(fileDialog.FileName, dt);
                    //ToExcel.SaveToExcel(fileDialog.FileName, dt);
                    DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                    this.gridControl设计点一览.ExportToXls(fileDialog.FileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
